package com.qianya.system.manage.web.login.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianya.model.pojo.login.qo.LoginDeptFindQO;
import com.qianya.model.pojo.login.qo.LoginDeptQO;
import com.qianya.model.pojo.login.vo.LoginDeptUserVO;
import com.qianya.model.pojo.login.vo.LoginDeptVO;
import com.qianya.model.pojo.login.vo.LoginUsersVO;
import com.qianya.system.manage.web.login.model.LoginDept;
import com.qianya.system.manage.web.login.service.ILoginDeptService;
import com.qianya.system.manage.web.login.mapper.LoginDeptMapper;
import org.springframework.stereotype.Service;
import com.qianya.model.common.CommonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianya.model.common.ResultCode;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 部门服务实现类
 * </p>
 *
 * @author lhl
 * @since 2020-07-02
 */
@Service
public class LoginDeptServiceImpl extends ServiceImpl<LoginDeptMapper, LoginDept> implements ILoginDeptService {
    @Resource
    private LoginDeptMapper loginDeptMapper;

    /**
     * 查找 部门
     *
     * @author lhl
     * @since 2020-07-02
     */
    @Override
    public CommonResult find(LoginDept loginDept) {
        List<LoginDept> list = list();
        try {
            //配置
            TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
            treeNodeConfig.setIdKey("deptId");
            treeNodeConfig.setNameKey("deptName");
            treeNodeConfig.setDeep(5);
            List<Tree<String>> treeNodes = TreeUtil.build(list, "0", treeNodeConfig,
                    (treeNode, tree) -> {
                        tree.setId(treeNode.getDeptId());
                        tree.setParentId(treeNode.getParDeptId());
                        tree.setName(treeNode.getDeptName());
                        tree.putExtra("deptStatus", treeNode.getDeptStatus());
                    });
            return CommonResult.success(treeNodes);
        } catch (Exception e) {
            return CommonResult.error(ResultCode.ERROR, "操作失败");
        }
    }

    /**
     * 新增 部门
     *
     * @author lhl
     * @since 2020-07-02
     */
    @Override
    public CommonResult create(LoginDept loginDept) {
        LoginDept dept= getOne(new QueryWrapper<LoginDept>().eq("dept_name",loginDept.getDeptName()));
        //LoginDeptVO loginDeptVO = loginDeptMapper.findByName(loginDept.getDeptName());
        if (dept != null) {
            return CommonResult.error(ResultCode.ERROR, "部门名称已存在，请重新输入");
        }
        boolean b = saveOrUpdate(loginDept);
        if (b) {
            return CommonResult.success();
        }
        return CommonResult.error(ResultCode.ERROR, "操作失败");
    }

    @Override
    public LoginDept findById(String deptId) {
        return loginDeptMapper.selectById(deptId);
    }

    @Override
    public CommonResult findAll(LoginDeptFindQO loginDeptQO) {

        IPage<LoginDeptUserVO> iPage = new Page<>(loginDeptQO.getPageNo(), loginDeptQO.getPageSize());

        iPage = loginDeptMapper.findAll(iPage, loginDeptQO);

        return CommonResult.success(iPage);
    }

    @Override
    public LoginDeptVO findByName(String deptName) {
        return loginDeptMapper.findByName(deptName);
    }

    @Override
    public CommonResult findAllDept() {
        return CommonResult.success(loginDeptMapper.findAllDept());
    }


    /**
     * 修改 部门
     *
     * @author lhl
     * @since 2020-07-02
     */
    @Override
    public CommonResult renewal(LoginDept loginDept, HttpServletRequest request) {
        LoginDept loginDeptVO = getOne(new QueryWrapper<LoginDept>().eq("dept_name",loginDept.getDeptName()).ne("dept_id",loginDept.getDeptId()));
        if (loginDeptVO != null) {
            return CommonResult.error(ResultCode.ERROR, "部门名称已存在，请重新输入");
        }
        boolean b = updateById(loginDept);
        if (b) {
            return CommonResult.success();
        }
        return CommonResult.error(ResultCode.ERROR, "操作失败");
    }
}
